package com.landiao.common.tag;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;

import com.landiao.common.SpringUtils;
import com.landiao.entities.NoteDict;
import com.landiao.service.NoteDictService;


/**
 * 
 * 选择下拉框
 * 
 */
public class NoteDictSelectTag extends TagSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2316877743270507380L;
	
	private String tabName;	//表名
	private String colName;	//字段名
	private String field;	//选择表单的Name  <select name="" id = "" />
	private String id;	//选择表单ID   <select name="" id = "" />
	private String defaultVal;	//默认值 
	private String dataClass;	//样式	<select dataClass name="selectName" id = "" />
	private boolean hasEmptyOption = false;	//是否需要空选项	<option value="">请选择</option>
	
	public int doStartTag() throws JspTagException {
		return EVAL_PAGE;
	}

	public int doEndTag() throws JspTagException {
		try {
			JspWriter out = this.pageContext.getOut();
			out.print(end().toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
	
	public StringBuffer end() {
		StringBuffer sb = new StringBuffer();
		NoteDictService service = SpringUtils.getBean(NoteDictService.class);
		NoteDict noteDict = new NoteDict();
		if(StringUtils.isNotBlank(colName)){
			noteDict.setColName(colName.toUpperCase());
		}
		if(StringUtils.isNotBlank(tabName)){
			noteDict.setTabName(tabName.toUpperCase());
		}
		List<NoteDict> list = service.getNoteDictList(noteDict);
		sb.append("<select name=\""+field+"\"");
		if (StringUtils.isNotBlank(this.id)) {
			 sb.append(" id=\""+id+"\"");
		}
//		if (StringUtils.isNotBlank(this.dataClass)) {
//			 sb.append(" "+this.dataClass);
//		}else{
//			sb.append(" data-am-selected=\"{btnSize: 'sm'}\"");
//		}
		sb.append(">");
		if (hasEmptyOption) {
			sb.append(" <option value=\"\">请选择</option>");
		}
		if (list != null && list.size()>0) {
			for (NoteDict dict : list) {
				if (dict.getVal1().equals(this.defaultVal)) {
					sb.append(" <option value=\""+dict.getVal1()+"\" selected=\"selected\">");
				}else {
					sb.append(" <option value=\""+dict.getVal1()+"\">");
				}
				sb.append(dict.getVal1Desc());
				sb.append(" </option>");
			}
		}
		sb.append("</select>");
		return sb;
	}

	public String getTabName() {
		return tabName;
	}

	public void setTabName(String tabName) {
		this.tabName = tabName;
	}

	public String getColName() {
		return colName;
	}

	public void setColName(String colName) {
		this.colName = colName;
	}

	public String getField() {
		return field;
	}

	public void setField(String field) {
		this.field = field;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getDefaultVal() {
		return defaultVal;
	}

	public void setDefaultVal(String defaultVal) {
		this.defaultVal = defaultVal;
	}

	public String getDataClass() {
		return dataClass;
	}

	public void setDataClass(String dataClass) {
		this.dataClass = dataClass;
	}

	public boolean isHasEmptyOption() {
		return hasEmptyOption;
	}

	public void setHasEmptyOption(boolean hasEmptyOption) {
		this.hasEmptyOption = hasEmptyOption;
	}
}
